<template>
  <div class="register">
    <slot />
    <el-form v-if="!turnRegister" ref="ruleForm" :model="ruleForm" :rules="registerRules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="账号" prop="username">
        <el-input v-model="ruleForm.username" placeholder="用作登录账号使用" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" show-password placeholder="请设置您的密码" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPassword">
        <el-input v-model="ruleForm.checkPassword" show-password placeholder="请确认您的密码" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email" placeholder="选填" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone" placeholder="请输入11位手机号" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="ruleForm.gender">
          <el-option label="男性" value="男性" />
          <el-option label="女性" value="女性" />
        </el-select>
      </el-form-item>
      <el-button type="primary" size="medium" style="width:100%;" @click="submitForm('ruleForm')">立即注册</el-button>
    </el-form>
  </div>
</template>

<script>
import { registerNewUser } from '@/api/register'
import { validatePhoneTwo } from '@/utils/validate'
export default {
  name: 'Register',
  props: ['turnRegister'],
  data() {
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    const validEmail = (rule, value, callback) => {
      const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
      if ((value === '' || value === undefined || value == null)) {
        callback()
      } else if ((!reg.test(value)) && value !== '') {
        callback(new Error('请输入正确的邮箱格式!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        checkPassword: '',
        email: '',
        gender: '男性',
        nickname: '',
        password: '',
        phone: '',
        username: ''
      },
      registerRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        checkPassword: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validatePhoneTwo }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个的字符', trigger: 'blur' }
        ],
        email: [
          { trigger: 'blur', validator: validEmail }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          registerNewUser(this.ruleForm.email, this.ruleForm.gender, this.ruleForm.nickname, this.ruleForm.password, this.ruleForm.phone, this.ruleForm.username)
            .then(() => { this.$emit('fillData', this.ruleForm.username, this.ruleForm.password) },
              error => { console.log(error) })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
